package com.quanyan.financialSystem.service.place.impl;

import com.quanyan.api.APIResponse;
import com.quanyan.financialSystem.entity.vo.place.resp.*;
import com.quanyan.financialSystem.mapper.ProfitLossReportFormMapper;
import com.quanyan.financialSystem.service.place.ProfitLossReportFormService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * ProfitLossReportFormServiceImpl
 *
 * @author Tu Chun
 * @date 2016/10/18
 */
@Service("profitLossStatementService")
public class ProfitLossReportFormServiceImpl implements ProfitLossReportFormService {
    private Logger log = LoggerFactory.getLogger(ProfitLossReportFormServiceImpl.class);
    @Autowired
    ProfitLossReportFormMapper profitLossReportFormMapper;

    @Override
    public APIResponse<ProfitLossInfoVos> getProfitLossReportForm(Map<String, Object> paramMap) {
        Long startTime = System.currentTimeMillis();

        /*List<MemberInsertAmountMap> memberInsertConsumeAmountMaps = profitLossReportFormMapper.getMemberInsertConsumeAmount(paramMap);
        Long endTime0 = System.currentTimeMillis();
        log.warn("盈亏报表会员充值消费耗时>>>>>" + (endTime0 - startTime) / 1000 + "s.");*/

        List<MemberInsertAmountMap> memberInsertAmountMaps = profitLossReportFormMapper.getMemberInsertAmount(paramMap);
        Long endTime1 = System.currentTimeMillis();
        log.warn("盈亏报表会员充值耗时>>>>>" + (endTime1 - startTime) / 1000 + "s.");

        List<PlaceSaleMap> placeSaleMaps = profitLossReportFormMapper.getPlaceSale(paramMap);
        Long endTime2 = System.currentTimeMillis();
        log.warn("盈亏报表场地销售金额耗时>>>>>" + (endTime2 - endTime1) / 1000 + "s.");
        List<EggAmountMap> eggAmountMaps = profitLossReportFormMapper.getEggAmount(paramMap);
        Long endTime3 = System.currentTimeMillis();
        log.warn("盈亏报表鸟蛋金额耗时>>>>>" + (endTime3 - endTime2) / 1000 + "s.");
        List<MemberConsumeInfoMap> memberConsumeInfoMaps = profitLossReportFormMapper.getMemberConsumeInfo(paramMap);
        Long endTime4 = System.currentTimeMillis();
        log.warn("盈亏报表会员消费金额耗时>>>>>" + (endTime4 - endTime3) / 1000 + "s.");
        List<InsertGiveAmountMap> insertGiveAmountMaps = profitLossReportFormMapper.getInsertGiveAmount(paramMap);
        Long endTime5 = System.currentTimeMillis();
        log.warn("盈亏报表会员赠送金额耗时>>>>>" + (endTime5 - endTime4) / 1000 + "s.");
        List<ConsumeGiveAmountMap> consumeGiveAmountMaps = profitLossReportFormMapper.getConsumeGiveAmount(paramMap);
        Long endTime6 = System.currentTimeMillis();
        log.warn("盈亏报表消费会员赠送金额耗时>>>>>" + (endTime6 - endTime5) / 1000 + "s.");
        List<VariousAmountMap> variousAmountMaps = profitLossReportFormMapper.getVariousAmount(paramMap);
        Long endTime7 = System.currentTimeMillis();
        log.warn("盈亏报表多种消费金额耗时>>>>>" + (endTime7 - endTime6) / 1000 + "s.");
        List<PlaceRentalAmountMap> placeRentalAmountMaps = profitLossReportFormMapper.getPlaceRentalAmount(paramMap);
        Long endTime8 = System.currentTimeMillis();
        log.warn("盈亏报表场租成本金额耗时>>>>>" + (endTime8 - endTime7) / 1000 + "s.");
        List<ProfitLossInfoVo> profitLossInfoVos = new ArrayList<ProfitLossInfoVo>();
        /**
         * 封装数据
         */
        int total1 = memberInsertAmountMaps.size();
        int total2 = placeSaleMaps.size();
        int total3 = eggAmountMaps.size();
        int total4 = memberConsumeInfoMaps.size();
        int total5 = insertGiveAmountMaps.size();
        int total6 = consumeGiveAmountMaps.size();
        int total7 = variousAmountMaps.size();
        int total8 = placeRentalAmountMaps.size();
        if (total1 != total3 || total1 != total7 || total1 != total8) {
            return APIResponse.returnFail("服务器错误");
        }
        if (total1 <= 0) {
            return APIResponse.returnFail("没有数据");
        }
        /**
         * 城市分界下标
         */
        List<Integer> flagIndex = new ArrayList<Integer>();
        ProfitLossInfoVo totalProfitLossInfoVo = new ProfitLossInfoVo();
        //初始化
        totalProfitLossInfoVo.setMemberCardGiftAmount(new BigDecimal(0.00));
        totalProfitLossInfoVo.setMemberInsertAmount(new BigDecimal(0.00));
        totalProfitLossInfoVo.setPlaceBusinessAmount(new BigDecimal(0.00));
        totalProfitLossInfoVo.setPlaceBusinessProfit(new BigDecimal(0.00));
        totalProfitLossInfoVo.setPlaceBusinessRatio(new BigDecimal(0.00));
        totalProfitLossInfoVo.setPlaceIncomeAmount(new BigDecimal(0.00));
        totalProfitLossInfoVo.setPlaceIncomeProfit(new BigDecimal(0.00));
        totalProfitLossInfoVo.setPlaceIncomeRatio(new BigDecimal(0.00));
        totalProfitLossInfoVo.setRentalCost(new BigDecimal(0.00));
        totalProfitLossInfoVo.setVariousSaleAmount(new BigDecimal(0.00));
        totalProfitLossInfoVo.setUseMemberCardGiftAmount(new BigDecimal(0.00));
        Integer beforeCityId = memberInsertAmountMaps.get(0).getCityId();
        for (int i = 0; i < total1; i++) {
            ProfitLossInfoVo profitLossInfoVo = new ProfitLossInfoVo();
            Integer cityId = memberInsertAmountMaps.get(i).getCityId();
            Integer categoryId = memberInsertAmountMaps.get(i).getCategoryId();
            if (cityId != beforeCityId) {
                flagIndex.add(i - 1);
                beforeCityId = cityId;
            }

            // 场租成本
            BigDecimal rentalCost = placeRentalAmountMaps.get(i).getPlaceRentalAmount() == null ? new BigDecimal(0.00) : placeRentalAmountMaps.get(i).getPlaceRentalAmount().setScale(2, RoundingMode.HALF_UP);
            // 会员卡充值金额
            BigDecimal memInsertAmount = memberInsertAmountMaps.get(i).getMemberInsertAmount() == null ? new BigDecimal(0.00) : memberInsertAmountMaps.get(i).getMemberInsertAmount().setScale(2, RoundingMode.HALF_UP);
            //BigDecimal memInsertAmount2 = memberInsertConsumeAmountMaps.get(i).getMemberInsertAmount2() == null ? new BigDecimal(0.00) : memberInsertAmountMaps.get(i).getMemberInsertAmount2();
            BigDecimal memberInsertAmount = memInsertAmount;


            BigDecimal mem_insert_op_before_give_amount = new BigDecimal(0.00);
            BigDecimal mem_insert_op_after_give_amount = new BigDecimal(0.00);
            // 会员卡赠送金额
            BigDecimal memberCardGiftAmount = new BigDecimal(0.00);
            if (insertGiveAmountMaps != null && insertGiveAmountMaps.size() > 0) {
                for (int k = 0; k < insertGiveAmountMaps.size(); k++) {
                    if (cityId == insertGiveAmountMaps.get(k).getCityId() && categoryId == insertGiveAmountMaps.get(k).getCategoryId()) {
                        mem_insert_op_before_give_amount = insertGiveAmountMaps.get(k).getInsert_op_before_give_amount() == null ? new BigDecimal(0.00) : insertGiveAmountMaps.get(k).getInsert_op_before_give_amount();
                        mem_insert_op_after_give_amount = insertGiveAmountMaps.get(k).getInsert_op_after_give_amount() == null ? new BigDecimal(0.00) : insertGiveAmountMaps.get(k).getInsert_op_after_give_amount();
                        memberCardGiftAmount = mem_insert_op_after_give_amount.subtract(mem_insert_op_before_give_amount);
                        break;
                    }

                }
            }


            //--会员优惠之前的价格
            //BigDecimal MemSellAmount = memberConsumeInfoMaps.get(i).getSellAmount() == null ? new BigDecimal(0.00) : memberConsumeInfoMaps.get(i).getSellAmount();
            //--会员实际付款金额
            BigDecimal MemPaidAmount = new BigDecimal(0.00);
            if (memberConsumeInfoMaps != null && memberConsumeInfoMaps.size() > 0) {
                for (int k = 0; k < memberConsumeInfoMaps.size(); k++) {
                    if (cityId == memberConsumeInfoMaps.get(k).getCityId() && categoryId == memberConsumeInfoMaps.get(k).getCategoryId()) {
                        /*//以前计算方式
                        BigDecimal opBeforeAmount = memberConsumeInfoMaps.get(k).getOp_before_amount() == null ? new BigDecimal(0.00) : memberConsumeInfoMaps.get(k).getOp_before_amount();
                        BigDecimal opAfterAmount = memberConsumeInfoMaps.get(k).getOp_after_amount() == null ? new BigDecimal(0.00) : memberConsumeInfoMaps.get(k).getOp_after_amount();
                        MemPaidAmount = opBeforeAmount.subtract(opAfterAmount).setScale(2, RoundingMode.HALF_UP);*/
                        MemPaidAmount = memberConsumeInfoMaps.get(k).getAmount() == null ? new BigDecimal(0.00) : memberConsumeInfoMaps.get(k).getAmount().setScale(2, RoundingMode.HALF_UP);
                        break;
                    }

                }
            }
            BigDecimal mem_consume_op_before_give_amount = new BigDecimal(0.00);
            BigDecimal mem_consume_op_after_give_amount = new BigDecimal(0.00);
            if (consumeGiveAmountMaps != null && consumeGiveAmountMaps.size() > 0) {
                for (int k = 0; k < consumeGiveAmountMaps.size(); k++) {
                    if (cityId == consumeGiveAmountMaps.get(k).getCityId() && categoryId == consumeGiveAmountMaps.get(k).getCategoryId()) {
                        mem_consume_op_before_give_amount = consumeGiveAmountMaps.get(k).getConsume_op_before_give_amount() == null ? new BigDecimal(0.00) : consumeGiveAmountMaps.get(k).getConsume_op_before_give_amount();
                        mem_consume_op_after_give_amount = consumeGiveAmountMaps.get(k).getConsume_op_after_give_amount() == null ? new BigDecimal(0.00) : consumeGiveAmountMaps.get(k).getConsume_op_after_give_amount();
                        break;
                    }

                }
            }
            //使用会员卡赠送金额
            BigDecimal useMemberCardGiftAmount = mem_consume_op_before_give_amount.subtract(mem_consume_op_after_give_amount);
            //--鸟蛋金额
            BigDecimal eggAmount = eggAmountMaps.get(i).getEggAmount() == null ? new BigDecimal(0.00) : eggAmountMaps.get(i).getEggAmount().setScale(2, RoundingMode.HALF_UP);

            //--优惠价格
            //BigDecimal totalDiscountAmount = eggAmount.add(MemSellAmount.subtract(MemPaidAmount));
            BigDecimal totalDiscountAmount = eggAmount;


            //场地营业金额
            //--场地销售金额
            BigDecimal placeSaleAmount = new BigDecimal(0.00);
            if (placeSaleMaps != null && placeSaleMaps.size() > 0) {
                for (int j = 0; j < placeSaleMaps.size(); j++) {
                    if (cityId == placeSaleMaps.get(j).getCityId() && categoryId == placeSaleMaps.get(j).getCategoryId()) {
                        placeSaleAmount = placeSaleMaps.get(j).getPlaceSaleAmount() == null ? new BigDecimal(0.00) : placeSaleMaps.get(j).getPlaceSaleAmount().setScale(2, RoundingMode.HALF_UP);
                        break;
                    }
                }
            }

            BigDecimal placeBusinessAmount = placeSaleAmount.add(memberInsertAmount).setScale(2, RoundingMode.HALF_UP);
            //场地收入金额
            BigDecimal placeIncomeAmount = placeSaleAmount.add(MemPaidAmount).setScale(2, RoundingMode.HALF_UP);
            //毛利（场地营业金额）
            BigDecimal placeBusinessProfit = placeBusinessAmount.subtract(rentalCost).setScale(2, RoundingMode.HALF_UP);
            //毛利（场地收入金额）
            BigDecimal placeIncomeProfit = placeIncomeAmount.subtract(rentalCost).setScale(2, RoundingMode.HALF_UP);
            // 投入产出比率（场地营业额）
            BigDecimal placeBusinessRatio = rentalCost.compareTo(new BigDecimal(0.00)) == 0 ? new BigDecimal(0.00) : placeBusinessProfit.multiply(new BigDecimal(100.00)).divide(rentalCost, 2).setScale(2, RoundingMode.HALF_UP);

            //投入产出比率（场地收入额）
            BigDecimal placeIncomeRatio = rentalCost.compareTo(new BigDecimal(0.00)) == 0 ? new BigDecimal(0.00) : placeIncomeProfit.multiply(new BigDecimal(100.00)).divide(rentalCost, 2).setScale(2, RoundingMode.HALF_UP);
            //多种经营金额
            BigDecimal VariousAmount = variousAmountMaps.get(i).getVariousAmount() == null ? new BigDecimal(0.00) : variousAmountMaps.get(i).getVariousAmount().setScale(2, RoundingMode.HALF_UP);
            BigDecimal variousSaleAmount = VariousAmount;

            //统计总计
            totalProfitLossInfoVo.setMemberCardGiftAmount(totalProfitLossInfoVo.getMemberCardGiftAmount() == null ? new BigDecimal(0.00) : totalProfitLossInfoVo.getMemberCardGiftAmount().add(memberCardGiftAmount).setScale(2, RoundingMode.HALF_UP));
            totalProfitLossInfoVo.setMemberInsertAmount(totalProfitLossInfoVo.getMemberInsertAmount() == null ? new BigDecimal(0.00) : totalProfitLossInfoVo.getMemberInsertAmount().add(memberInsertAmount).setScale(2, RoundingMode.HALF_UP));
            totalProfitLossInfoVo.setPlaceBusinessAmount(totalProfitLossInfoVo.getPlaceBusinessAmount() == null ? new BigDecimal(0.00) : totalProfitLossInfoVo.getPlaceBusinessAmount().add(placeBusinessAmount).setScale(2, RoundingMode.HALF_UP));
            totalProfitLossInfoVo.setPlaceBusinessProfit(totalProfitLossInfoVo.getPlaceBusinessProfit() == null ? new BigDecimal(0.00) : totalProfitLossInfoVo.getPlaceBusinessProfit().add(placeBusinessProfit).setScale(2, RoundingMode.HALF_UP));
            //totalProfitLossInfoVo.setPlaceBusinessRatio(totalProfitLossInfoVo.getPlaceBusinessRatio() == null ? new BigDecimal(0.00) : totalProfitLossInfoVo.getPlaceBusinessRatio().add(placeBusinessRatio));
            totalProfitLossInfoVo.setPlaceIncomeAmount(totalProfitLossInfoVo.getPlaceIncomeAmount() == null ? new BigDecimal(0.00) : totalProfitLossInfoVo.getPlaceIncomeAmount().add(placeIncomeAmount).setScale(2, RoundingMode.HALF_UP));
            totalProfitLossInfoVo.setPlaceIncomeProfit(totalProfitLossInfoVo.getPlaceIncomeProfit() == null ? new BigDecimal(0.00) : totalProfitLossInfoVo.getPlaceIncomeProfit().add(placeIncomeProfit).setScale(2, RoundingMode.HALF_UP));
            //totalProfitLossInfoVo.setPlaceIncomeRatio(totalProfitLossInfoVo.getPlaceIncomeRatio() == null ? new BigDecimal(0.00) : totalProfitLossInfoVo.getPlaceIncomeRatio().add(placeIncomeRatio));
            totalProfitLossInfoVo.setRentalCost(totalProfitLossInfoVo.getRentalCost() == null ? new BigDecimal(0.00) : totalProfitLossInfoVo.getRentalCost().add(rentalCost).setScale(2, RoundingMode.HALF_UP));
            totalProfitLossInfoVo.setVariousSaleAmount(totalProfitLossInfoVo.getVariousSaleAmount() == null ? new BigDecimal(0.00) : totalProfitLossInfoVo.getVariousSaleAmount().add(variousSaleAmount).setScale(2, RoundingMode.HALF_UP));
            totalProfitLossInfoVo.setUseMemberCardGiftAmount(totalProfitLossInfoVo.getUseMemberCardGiftAmount() == null ? new BigDecimal(0.00) : totalProfitLossInfoVo.getUseMemberCardGiftAmount().add(useMemberCardGiftAmount).setScale(2, RoundingMode.HALF_UP));

            profitLossInfoVo.setCityId(memberInsertAmountMaps.get(i).getCityId());
            profitLossInfoVo.setSearchStartPayDate(memberInsertAmountMaps.get(i).getSearchStartTime());
            profitLossInfoVo.setSearchEndPayDate(memberInsertAmountMaps.get(i).getSearchEndTime());
            profitLossInfoVo.setCategoryId(memberInsertAmountMaps.get(i).getCategoryId());
            profitLossInfoVo.setMemberCardGiftAmount(memberCardGiftAmount);
            profitLossInfoVo.setMemberInsertAmount(memberInsertAmount);
            profitLossInfoVo.setPlaceBusinessAmount(placeBusinessAmount);
            profitLossInfoVo.setPlaceBusinessProfit(placeBusinessProfit);
            profitLossInfoVo.setPlaceBusinessRatio(placeBusinessRatio);
            profitLossInfoVo.setPlaceIncomeAmount(placeIncomeAmount);
            profitLossInfoVo.setPlaceIncomeProfit(placeIncomeProfit);
            profitLossInfoVo.setPlaceIncomeRatio(placeIncomeRatio);
            profitLossInfoVo.setRentalCost(rentalCost);
            profitLossInfoVo.setVariousSaleAmount(variousSaleAmount);
            profitLossInfoVo.setUseMemberCardGiftAmount(useMemberCardGiftAmount);
            profitLossInfoVo.setType((byte) 0);
            profitLossInfoVos.add(profitLossInfoVo);

        }
        // 投入产出总比率（场地营业额）
        BigDecimal placeBusinessRatio = totalProfitLossInfoVo.getRentalCost().compareTo(new BigDecimal(0.00)) == 0 ? new BigDecimal(0.00) : totalProfitLossInfoVo.getPlaceBusinessProfit().multiply(new BigDecimal(100.00)).divide(totalProfitLossInfoVo.getRentalCost(), 2);
        //投入产出总比率（场地收入额）
        BigDecimal placeIncomeRatio = totalProfitLossInfoVo.getRentalCost().compareTo(new BigDecimal(0.00)) == 0 ? new BigDecimal(0.00) : totalProfitLossInfoVo.getPlaceIncomeProfit().multiply(new BigDecimal(100.00)).divide(totalProfitLossInfoVo.getRentalCost(), 2);
        totalProfitLossInfoVo.setPlaceBusinessRatio(placeBusinessRatio);
        totalProfitLossInfoVo.setPlaceIncomeRatio(placeIncomeRatio);
        List<ProfitLossInfoVo> cityProfitLossInfoVoList = new ArrayList<ProfitLossInfoVo>();
        //记录最后一个城市下标
        flagIndex.add(total1 - 1);
        //统计城市总计
        int skipNum = 0;
        int startflag = 0;

        for (int i = 0; i < flagIndex.size(); i++) {
            if (i == 0) {
                startflag = 0;
            } else {
                startflag = flagIndex.get(i - 1) + 1 + skipNum;
            }
            ProfitLossInfoVo cityProfitLossInfoVo = new ProfitLossInfoVo();
            cityProfitLossInfoVo.setCityId(profitLossInfoVos.get(flagIndex.get(i) + skipNum).getCityId());
            cityProfitLossInfoVo.setSearchStartPayDate(profitLossInfoVos.get(flagIndex.get(i) + skipNum).getSearchStartPayDate());
            cityProfitLossInfoVo.setSearchEndPayDate(profitLossInfoVos.get(flagIndex.get(i) + skipNum).getSearchEndPayDate());
            cityProfitLossInfoVo.setType((byte) 1);
            //初始化
            cityProfitLossInfoVo.setMemberCardGiftAmount(new BigDecimal(0.00));
            cityProfitLossInfoVo.setMemberInsertAmount(new BigDecimal(0.00));
            cityProfitLossInfoVo.setPlaceBusinessAmount(new BigDecimal(0.00));
            cityProfitLossInfoVo.setPlaceBusinessProfit(new BigDecimal(0.00));
            cityProfitLossInfoVo.setPlaceBusinessRatio(new BigDecimal(0.00));
            cityProfitLossInfoVo.setPlaceIncomeAmount(new BigDecimal(0.00));
            cityProfitLossInfoVo.setPlaceIncomeProfit(new BigDecimal(0.00));
            cityProfitLossInfoVo.setPlaceIncomeRatio(new BigDecimal(0.00));
            cityProfitLossInfoVo.setRentalCost(new BigDecimal(0.00));
            cityProfitLossInfoVo.setVariousSaleAmount(new BigDecimal(0.00));
            cityProfitLossInfoVo.setUseMemberCardGiftAmount(new BigDecimal(0.00));
            for (int j = startflag; j <= flagIndex.get(i) + skipNum; j++) {
                cityProfitLossInfoVo.setMemberCardGiftAmount(cityProfitLossInfoVo.getMemberCardGiftAmount().add(profitLossInfoVos.get(j).getMemberCardGiftAmount().setScale(2, RoundingMode.HALF_UP)));
                cityProfitLossInfoVo.setMemberInsertAmount(cityProfitLossInfoVo.getMemberInsertAmount().add(profitLossInfoVos.get(j).getMemberInsertAmount().setScale(2, RoundingMode.HALF_UP)));
                cityProfitLossInfoVo.setPlaceBusinessAmount(cityProfitLossInfoVo.getPlaceBusinessAmount().add(profitLossInfoVos.get(j).getPlaceBusinessAmount().setScale(2, RoundingMode.HALF_UP)));
                cityProfitLossInfoVo.setPlaceBusinessProfit(cityProfitLossInfoVo.getPlaceBusinessProfit().add(profitLossInfoVos.get(j).getPlaceBusinessProfit().setScale(2, RoundingMode.HALF_UP)));
                //cityProfitLossInfoVo.setPlaceBusinessRatio(cityProfitLossInfoVo.getPlaceBusinessRatio() == null ? new BigDecimal(0.00) : cityProfitLossInfoVo.getPlaceBusinessRatio().add(profitLossInfoVos.get(j).getPlaceBusinessRatio()));
                cityProfitLossInfoVo.setPlaceIncomeAmount(cityProfitLossInfoVo.getPlaceIncomeAmount() == null ? new BigDecimal(0.00) : cityProfitLossInfoVo.getPlaceIncomeAmount().add(profitLossInfoVos.get(j).getPlaceIncomeAmount().setScale(2, RoundingMode.HALF_UP)));
                cityProfitLossInfoVo.setPlaceIncomeProfit(cityProfitLossInfoVo.getPlaceIncomeProfit() == null ? new BigDecimal(0.00) : cityProfitLossInfoVo.getPlaceIncomeProfit().add(profitLossInfoVos.get(j).getPlaceIncomeProfit().setScale(2, RoundingMode.HALF_UP)));
                //cityProfitLossInfoVo.setPlaceIncomeRatio(cityProfitLossInfoVo.getPlaceIncomeRatio() == null ? new BigDecimal(0.00) : cityProfitLossInfoVo.getPlaceIncomeRatio().add(profitLossInfoVos.get(j).getPlaceIncomeRatio()));
                cityProfitLossInfoVo.setRentalCost(cityProfitLossInfoVo.getRentalCost() == null ? new BigDecimal(0.00) : cityProfitLossInfoVo.getRentalCost().add(profitLossInfoVos.get(j).getRentalCost().setScale(2, RoundingMode.HALF_UP)));
                cityProfitLossInfoVo.setVariousSaleAmount(cityProfitLossInfoVo.getVariousSaleAmount() == null ? new BigDecimal(0.00) : cityProfitLossInfoVo.getVariousSaleAmount().add(profitLossInfoVos.get(j).getVariousSaleAmount().setScale(2, RoundingMode.HALF_UP)));
                cityProfitLossInfoVo.setUseMemberCardGiftAmount(cityProfitLossInfoVo.getUseMemberCardGiftAmount() == null ? new BigDecimal(0.00) : cityProfitLossInfoVo.getUseMemberCardGiftAmount().add(profitLossInfoVos.get(j).getUseMemberCardGiftAmount().setScale(2, RoundingMode.HALF_UP)));
            }
            // 投入产出城市总比率（场地营业额）
            BigDecimal cityPlaceBusinessRatio = cityProfitLossInfoVo.getRentalCost().compareTo(new BigDecimal(0.00)) == 0 ? new BigDecimal(0.00) : cityProfitLossInfoVo.getPlaceBusinessProfit().multiply(new BigDecimal(100.00)).divide(cityProfitLossInfoVo.getRentalCost(), 2);
            //投入产出城市总比率（场地收入额）
            BigDecimal cityPlaceIncomeRatio = cityProfitLossInfoVo.getRentalCost().compareTo(new BigDecimal(0.00)) == 0 ? new BigDecimal(0.00) : cityProfitLossInfoVo.getPlaceIncomeProfit().multiply(new BigDecimal(100.00)).divide(cityProfitLossInfoVo.getRentalCost(), 2);
            cityProfitLossInfoVo.setPlaceBusinessRatio(cityPlaceBusinessRatio);
            cityProfitLossInfoVo.setPlaceIncomeRatio(cityPlaceIncomeRatio);
            profitLossInfoVos.add(flagIndex.get(i) + 1 + skipNum, cityProfitLossInfoVo);
            //跳过添加的数
            ++skipNum;
        }


        ProfitLossInfoVos profitLossInfoVosStatistics = new ProfitLossInfoVos();
        profitLossInfoVosStatistics.setProfitLossInfoVos(profitLossInfoVos);
        profitLossInfoVosStatistics.setTotalProfitLossInfoVo(totalProfitLossInfoVo);
        Long endTime = System.currentTimeMillis();
        log.warn("盈亏报表接口总耗时>>>>>" + (endTime - startTime) / 1000 + "s.");
        return APIResponse.returnSuccess(profitLossInfoVosStatistics);
    }
}
